//
//  SearchViewController.m
//  一点互助
//
//  Created by 张泊秋 on 16/8/19.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchHeaderView.h"
#import "FTHomeTableViewCell.h"
#import "ResponseManager.h"
#import "WishesModel.h"
#import "ConfigDefine.h"
#import "DetailViewController.h"

@interface SearchViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataSource;
@property (strong, nonatomic) SearchHeaderView *headerView;
@property (strong, nonatomic) UISearchBar *searchBar;
@property (strong, nonatomic) UIBarButtonItem *searchBtn;
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupBackItem];
    self.navigationItem.titleView = self.searchBar;
    self.navigationItem.rightBarButtonItem = self.searchBtn;
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [self.view addSubview:self.tableView];
    UINib *nib = [UINib nibWithNibName:@"FTHomeTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"HomeTableViewCell"];
    self.headerView = [SearchHeaderView getHeaderView];
    self.tableView.tableHeaderView = self.headerView;
    [self getRequest];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.tabBarController.tabBar setHidden:NO];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
}
-(void)getRequest{
    __weak typeof(self) myself = self;
    [ResponseManager startGetRequestWithURLString:kHotWishWordURL parameters:nil completionHandler:^(FBResponse *response) {
        if (response.success) {
            PLLOG(@"%@",response.resultValue);
            myself.headerView.label1.text = response.resultValue[0];
            myself.headerView.label2.text = response.resultValue[1];
            myself.headerView.label3.text = response.resultValue[2];
        }
    }];
}
-(void)goSearch{
    __weak typeof(self) myself = self;
    NSDictionary *parameter = @{@"search":self.searchBar.text};
    [ResponseManager startGetRequestWithURLString:kGetWishListURL parameters:parameter completionHandler:^(FBResponse *response) {
        if (response.success) {
            PLLOG(@"%@",response.resultValue);
            myself.dataSource = [[WishesModel modelArrayWithDictionaryArr:response.resultValue] mutableCopy];
            [myself.tableView reloadData];
        }
    }];

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *vc = [[DetailViewController alloc]init];
    vc.model = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100 * kScreenHeightScale;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FTHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTableViewCell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
-(UISearchBar *)searchBar{
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-100, 30)];
        _searchBar.delegate = self;
        _searchBar.placeholder = @"请输入关键字";
        _searchBar.layer.cornerRadius = 5;
    }
    return _searchBar;
}
-(UIBarButtonItem *)searchBtn{
    if (!_searchBtn) {
        _searchBtn = [[UIBarButtonItem alloc]initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(goSearch)];
    }
    return _searchBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
